﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.RuneMaster.Core;
using Studio77.RuneMaster.Views;
using Studio77.RuneMaster.Entities;

namespace Studio77.RuneMaster.Pages
{
    public partial class ToolChoosePage : PhoneApplicationPage
    {
        public static ToolChoosePage Instance = null;

        public ToolChoosePage()
        {
            InitializeComponent();
            Instance = this;
            this.Loaded += ToolChoosePage_Loaded;
        }

        void ToolChoosePage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= ToolChoosePage_Loaded;
            tools.Clear();

            tools.Add(toolAdd500);
            tools.Add(toolAddSpirit);
            tools.Add(toolCollSpirit);
            tools.Add(toolReset);
            tools.Add(toolScore2);
            tools.Add(toolSpirit2);

            usedTools.Clear();
            usedTools.Add(selectTool1);
            usedTools.Add(selectTool2);
            usedTools.Add(selectTool3);
            usedTools.Add(selectTool4);

            slotUseStatus.Clear();
            slotUseStatus.Add(0, false);
            slotUseStatus.Add(1, false);
            slotUseStatus.Add(2, false);
            slotUseStatus.Add(3, false);
            foreach (var item in tools)
            {
                item.SetToolCount(StageManager.Instance.ToolInfos[item.ToolTypeValue].Count);
                item.SetUnUsed();
                item.SetUnselect();
            }

            foreach (var item in usedTools)
            {
                item.Visibility = System.Windows.Visibility.Collapsed;
            }

            this.txtWaring.Visibility = System.Windows.Visibility.Collapsed;
        }

        List<ToolItem> tools = new List<ToolItem>();
        Dictionary<int, bool> slotUseStatus = new Dictionary<int, bool>();
        List<ToolItem> usedTools = new List<ToolItem>();
        int currentTotalCost = 0;

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //更新界面显示
            if (StageManager.Instance.CurrentGameMode == "e")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Visible;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (StageManager.Instance.CurrentGameMode == "n")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Visible;
            }
            else if (StageManager.Instance.CurrentGameMode == "h")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Visible;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Collapsed;
            }

            this.txtStageName.Text = "Stage " + StageManager.Instance.CurrentStageIndex;
            this.txtTotalG.Text = StageManager.Instance.TotalG + " G";
        }

        public void SelectTool(ToolType type)
        {
            foreach (var item in tools)
            {
                if (type == item.ToolTypeValue)
                {
                    item.SetSelect();
                    //增加到slot

                    if (!item.isUsed)
                    {
                        int slot = -1;
                        foreach (var slotKey in slotUseStatus.Keys)
                        {
                            if (!slotUseStatus[slotKey])
                            {
                                //可以使用该slot
                                slot = slotKey;
                                break;
                            }
                        }

                        if (slot != -1)
                        {
                            item.SetUsed();
                            slotUseStatus[slot] = true;
                            usedTools[slot].Visibility = System.Windows.Visibility.Visible;
                            usedTools[slot].SetTool(item.ToolTypeValue);
                            usedTools[slot].SetToolCount(item.count);
                            CalcTotalCost();
                        }

                    }

                }
                else
                {
                    item.SetUnselect();
                }
            }

            //显示详情
            ToolInfo toolInfo = StageManager.Instance.ToolInfos[type];
            this.txtToolCost.Text = "COST: " + toolInfo.CostG + " G";
            this.txtToolCount.Text = "(" + toolInfo.Count + " packs)";
            this.txtToolDescribe.Text = toolInfo.Desc;
            this.txtToolName.Text = toolInfo.Name;
        }

        private void btnStartStage_Click_1(object sender, RoutedEventArgs e)
        {
            //TODO lc 需要检查道具购买的状态
            if (StageManager.Instance.TotalG >= currentTotalCost)
            {
                StageManager.Instance.TotalG -= currentTotalCost;
                StageManager.Instance.CurrentRestartChange = 3;
                StageManager.Instance.ClearUsedTool();

                SoundManager.PlayEffect(SoundManager.buttonClick);

                //设置游戏中的道具信息
                int gameSlotIndex = 0;
                foreach (var slotIndex in slotUseStatus.Keys)
                {
                    if (slotUseStatus[slotIndex])
                    {
                        //有道具
                        StageManager.Instance.CurrentToolSlotInfo[gameSlotIndex] = new ToolSlotInfo()
                        {
                            Type = usedTools[slotIndex].ToolTypeValue,
                            LeftCount = usedTools[slotIndex].count
                        };
                        gameSlotIndex++;
                    }
                    else
                    {
                        StageManager.Instance.CurrentToolSlotInfo[gameSlotIndex] = null;
                    }

                }

                this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?isNewGame=1", UriKind.RelativeOrAbsolute));
            }
        }

        internal void UnUseThisTool(ToolItem toolItem)
        {
            toolItem.Visibility = System.Windows.Visibility.Collapsed;
            int slot = usedTools.IndexOf(toolItem);
            foreach (var item in tools)
            {
                if (item.ToolTypeValue == toolItem.ToolTypeValue)
                {
                    item.SetUnUsed();
                    slotUseStatus[slot] = false;
                    break;
                }
            }
            CalcTotalCost();
        }

        private void CalcTotalCost()
        {
            int sum = 0;
            foreach (var slot in slotUseStatus.Keys)
            {
                if (slotUseStatus[slot])
                {
                    ToolType type = usedTools[slot].ToolTypeValue;
                    sum += StageManager.Instance.ToolInfos[type].CostG;
                }
            }
            this.txtTotalCostG.Text = "-" + sum + " G";
            currentTotalCost = sum;

            if (StageManager.Instance.TotalG >= currentTotalCost)
            {
                this.btnStartStage.IsEnabled = true;
                this.txtWaring.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnStartStage.IsEnabled = false;
                this.txtWaring.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }
}